package com.qiaoanju.web.security.service;

import com.qiaoanju.back.domain.system.UserModel;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Date;

/**
 * Created by eversec on 2017/9/28.
 */
public class LoginUserService {


    // 获取登入用户名
    public static String getLoginUserName() {

        if(getLoginUser() != null){
            return getLoginUser().getUsername();
        }else{
            return "";
        }


    }

    /**
     * 获取登录用户信息
     * @return
     */
    public static UserModel getLoginUser() {

        //向上转型
        UserModel userDetails = (UserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(userDetails != null){
            //设置登录时间
            userDetails.setUpdate_time(new Date());
            return userDetails ;
        }else {
            return null ;
        }
    }

    /**
     * 获取当前登录用户的id
     * @return
     */
    public static int getLoginUserId(){

        if(getLoginUser() != null){
            return getLoginUser().getId();
        }else{
            return 0 ;
        }


    }


}
